package com.mars.test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.mybatis.generator.api.GeneratedXmlFile;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

public class Generator {

	@Test
	public void wx() {
		generator("generator/generator.xml");
	}
	
	public void generator(String generatorFile) {
		try {
			InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(generatorFile) ;
			List<String> warnings = new ArrayList<String>();
			ConfigurationParser cp = new ConfigurationParser(warnings);
			Configuration config = cp.parseConfiguration(resourceAsStream);
			DefaultShellCallback callback = new DefaultShellCallback(true);
			MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
			myBatisGenerator.generate(null);
			
			List<GeneratedXmlFile> generatedXmlFiles = myBatisGenerator.getGeneratedXmlFiles();
			for (GeneratedXmlFile generatedXmlFile : generatedXmlFiles) {
				System.out.println(generatedXmlFile.getTargetProject()+"/"+generatedXmlFile.getTargetPackage()+"/"+generatedXmlFile.getFileName());	
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
